package Aleehot100;

import java.util.Arrays;

/**
 * ClassName: c018
 * Package: Aleehot100
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/5/29 14:19
 * @Version 1.0
 * @Since 1.0
 */
public class c018 {
    public static void main(String[] args) {
       int[][] arr =  {{1,1,1},{1,0,1},{1,1,1}};
       setZeroes(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.println(Arrays.toString(arr[i]));
        }

    }

    public static void setZeroes(int[][] matrix) {
        boolean isCol0 = false;
        int n = matrix.length, m = matrix[0].length;
        for (int i = 0; i < n; i++) {
            if(!isCol0 && matrix[i][0] == 0) {
                isCol0 = true;
            }
            // 设置0元素,
            for (int j = 1; j < m; j++) {
                if(matrix[i][j] == 0) {
                    matrix[i][0] = 0;
                    matrix[0][j] = 0;
                }
            }
        }

        // 设置数据
        for (int i = n - 1; i >= 0 ; i--) {
            for (int j = 1; j < m; j++) {
                if(matrix[i][0] == 0 || matrix[0][j] == 0) {
                    matrix[i][j] = 0;
                }
            }
            if(isCol0) {
                matrix[i][0] = 0;
            }
        }
    }
}
